package com.moss.cloud.plugins.biz.controller;

import com.moss.cloud.common.core.constant.ServiceList;
import com.moss.cloud.common.core.model.Result;
import com.moss.cloud.common.security.annotation.SysLog;
import com.moss.cloud.plugins.api.model.from.BaseFrom;
import com.moss.cloud.plugins.api.model.from.GenerationCodeFrom;
import com.moss.cloud.plugins.api.model.from.ModelDesignFrom;
import com.moss.cloud.plugins.api.model.from.ProjectCodeFrom;
import com.moss.cloud.plugins.api.model.param.ModelDesignParam;
import com.moss.cloud.plugins.biz.service.IModelDesignService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 模型设计前端控制器
 *
 * @author 瑾年
 * @date 2024年1月3日
 */
@RestController
@RequestMapping(ServiceList.PLUGINS + "/modelDesign")
public class ModelDesignController {

    private final IModelDesignService modelDesignService;

    public ModelDesignController(IModelDesignService modelDesignService) {
        this.modelDesignService = modelDesignService;
    }

    /**
     * 模型分页查询
     *
     * @param param
     * @return
     */
    @PostMapping("/page")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "模型分页查询")
    public Result<?> page(@RequestBody @Validated ModelDesignParam param) {
        return Result.success(modelDesignService.page(param));
    }

    /**
     * 新增模型设计
     *
     * @param modelDesignFrom
     * @return
     */
    @PostMapping("/saveModelDesign")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "新增模型设计")
    public Result<?> saveModelDesign(@RequestBody @Validated ModelDesignFrom modelDesignFrom) {
        return Result.success(modelDesignService.saveModelDesign(modelDesignFrom));
    }

    /**
     * 更新模型设计
     *
     * @param modelDesignFrom
     * @return
     */
    @PostMapping("/updateModelDesign")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "更新模型设计")
    public Result<?> updateModelDesign(@RequestBody @Validated ModelDesignFrom modelDesignFrom) {
        return Result.success(modelDesignService.updateModelDesign(modelDesignFrom));
    }

    /**
     * 删除模型
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/removeModelDesign")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "删除模型")
    public Result<?> removeModelDesign(@RequestBody @Validated BaseFrom baseFrom) {
        return Result.success(modelDesignService.removeModelDesign(baseFrom));
    }

    /**
     * 模型详情
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/modeDetail")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "模型详情")
    public Result<?> modeDetail(@RequestBody @Validated BaseFrom baseFrom) {
        return Result.success(modelDesignService.modeDetail(baseFrom));
    }

    /**
     * 获取SQL脚本
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/getSqlScript")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "获取SQL脚本")
    public Result<?> getSqlScript(@RequestBody @Validated BaseFrom baseFrom) {
        return Result.success(modelDesignService.getSqlScript(baseFrom));
    }

    /**
     * 同步数据源
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/syncDataSource")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "同步数据源")
    public Result<?> syncDataSource(@RequestBody @Validated BaseFrom baseFrom) {
        return Result.success(modelDesignService.syncDataSource(baseFrom));
    }

    /**
     * 撤回同步
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/recallSync")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "撤回同步")
    public Result<?> recallSync(@RequestBody @Validated BaseFrom baseFrom) {
        return Result.success(modelDesignService.recallSync(baseFrom));
    }

    /**
     * 生成代码
     *
     * @param generationCodeFrom
     * @param response
     * @return
     */
    @PostMapping("/generateCode")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "生成代码")
    public void generateCode(HttpServletResponse response, @RequestBody @Validated GenerationCodeFrom generationCodeFrom) {
        modelDesignService.generateCode(response, generationCodeFrom);
    }

    /**
     * 标准项目模板生成
     *
     * @param response
     * @param projectCodeFrom
     */
    @PostMapping("/genProject")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_PLUGINS, operation = "标准项目模板生成")
    public void genProject(HttpServletResponse response, @RequestBody @Validated ProjectCodeFrom projectCodeFrom) {
        modelDesignService.genProject(response, projectCodeFrom);
    }
}
